<template>
  <div class="register">
    <el-form ref="passwdRef" :model="passwdForm" :rules="passwdRules" class="register-form">
      <h3 class="title">{{ title }}</h3>
      <el-form-item prop="username">
        <el-input v-model="passwdForm.username" type="text" auto-complete="off" placeholder="账号" disabled>
          <template #prefix><svg-icon icon-class="user" class="el-input__icon input-icon" /></template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="passwdForm.password" type="password" auto-complete="off" placeholder="密码">
          <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
        </el-input>
      </el-form-item>
      <el-form-item prop="newPassword">
        <el-input v-model="passwdForm.newPassword" type="password" auto-complete="off" placeholder="新密码">
          <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
        </el-input>
      </el-form-item>
      <el-form-item prop="confirmPassword">
        <el-input v-model="passwdForm.confirmPassword" type="password" auto-complete="off" placeholder="确认密码">
          <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
        </el-input>
      </el-form-item>
      <!--
      <el-form-item prop="code" v-if="captchaEnabled">
        <el-input
            v-model="passwdForm.code"
            auto-complete="off"
            placeholder="验证码"
            style="width: 63%"
        >
          <template #prefix><svg-icon icon-class="validCode" class="el-input__icon input-icon" /></template>
        </el-input>
        <div class="register-code">
          <img :src="codeUrl" @click="getCode" class="register-code-img" alt="" />
        </div>
      </el-form-item>
      -->
      <el-form-item style="width:100%;">
        <div style="width:100%;justify-content: space-between;display: flex;">
          <el-button :loading="loading" type="primary" style="width:100%;" @click.prevent="handleModifyPassword">
            <span v-if="!loading">修改密码</span>
            <span v-else>请 稍 候...</span>
          </el-button>
          <el-button style="width:100%;" @click="useUserStore().logout()">
            <span>退出系统</span>
          </el-button>
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-register-footer">
      <span>Copyright © 2023 caizhh All Rights Reserved.</span>
    </div>
  </div>
</template>

<script setup>
import {ElMessageBox} from "element-plus";
import {useRouter} from "vue-router";
import {getCurrentInstance, ref} from "vue";
import settings from "@/settings";
import useUserStore from "@/store/modules/user";
import {doPost} from "@/config/http";
import {alertErrorMsg, isNullOrUndefined, svc} from "@/config/common";
import {getUserId} from "@/config/cookie";

const title = settings.title
const router = useRouter();
const { proxy } = getCurrentInstance();

const passwdForm = ref({
  username: useUserStore().username,
  password: "",
  newPassword: "",
  confirmPassword: "",
  code: "",
  uuid: ""
});

const equalToPassword = (rule, value, callback) => {
  if (passwdForm.value.newPassword !== value) {
    callback(new Error("两次输入的密码不一致"));
  } else {
    callback();
  }
};

const passwdRules = {
  username: [
    { required: true, trigger: "blur", message: "请输入您的账号" },
    { min: 2, max: 20, message: "用户账号长度必须介于 2 和 20 之间", trigger: "blur" }
  ],
  password: [
    { required: true, trigger: "blur", message: "请输入密码" },
    { min: 5, max: 20, message: "用户密码长度必须介于 5 和 20 之间", trigger: "blur" }
  ],
  newPassword: [
    { required: true, trigger: "blur", message: "请输入新密码" },
    { min: 5, max: 20, message: "用户密码长度必须介于 5 和 20 之间", trigger: "blur" }
  ],
  confirmPassword: [
    { required: true, trigger: "blur", message: "请再次输入您的密码" },
    { required: true, validator: equalToPassword, trigger: "blur" }
  ],
  code: [{ required: true, trigger: "change", message: "请输入验证码" }]
};

const codeUrl = ref("");
const loading = ref(false);
const captchaEnabled = ref(true);

async function handleModifyPassword() {
  const formName = 'passwdRef'
  let v = await proxy.$refs[formName].validate().catch((err)=>{return err})
  if (v !== true) {
    alertErrorMsg('表单验证不通过，请检查数据。')
    return
  }
  loading.value = true;
  const data = {
    userId: getUserId(),
    password: passwdForm.value.password,
    newPassword: passwdForm.value.newPassword,
  }
  doPost(svc, "/custom/updateUserPwd", data).then(_ => {
    proxy.$modal.msgSuccess("修改成功");
    ElMessageBox.alert("操作成功", "系统提示", {
      dangerouslyUseHTMLString: true,
      type: "success",
    }).then(() => {
      useUserStore().logout().then(() => {
        location.href = '/index';
      })
    }).catch(() => {});
  }).catch(() => {
    loading.value = false;
    if (captchaEnabled) {
      getCode();
    }
  });
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": "c5d7bdfcf0a342859329d9869f2768c0"
  }
  captchaEnabled.value = isNullOrUndefined(res.captchaEnabled) ? true : res.captchaEnabled;
  if (captchaEnabled.value) {
    codeUrl.value = "data:image/gif;base64," + res.img;
    passwdForm.value.uuid = res.uuid;
  }
  /*
  getCodeImg().then(res => {
    captchaEnabled.value = isNullOrUndefined(res.captchaEnabled) ? true : res.captchaEnabled;
    if (captchaEnabled.value) {
      codeUrl.value = "data:image/gif;base64," + res.img;
      passwdForm.value.uuid = res.uuid;
    }
  });
  */
}

getCode();
</script>

<style lang='scss' scoped>
.register {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
}
.title {
  margin: 0 auto 30px auto;
  text-align: center;
  color: #707070;
}

.register-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 40px;
    input {
      height: 40px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 0;
  }
}
.register-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.register-code {
  width: 33%;
  height: 40px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-register-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial,serif;
  font-size: 12px;
  letter-spacing: 1px;
}
.register-code-img {
  height: 40px;
  padding-left: 12px;
}
</style>
